package Demo11;

import java.util.*;

class List {
    String name;
    int space;//所需内存空间
    int time;//运行时间
    int priority;//优先数
    int sort;//输入顺序
    int return_space;//归还空间

    public List(String name, int space, int time, int priority, int sort) {
        this.name = name;
        this.space = space;
        this.time = time;
        this.priority = priority;
        this.sort = sort;
    }


}

public class Main {
    static int sum_memory = 100;
    static ArrayList<Integer> arrayList = new ArrayList<>();


    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        List[] list = new List[3];
        for (int i = 0; i < 3; i++) {
            String name = s.next();//进程名
            int space = s.nextInt();//所需内存空间
            int time = s.nextInt();//运行时间
            int priority = s.nextInt();//优先数
            int sort = i;//输入顺序
            list[i] = new List(name, space, time, priority, sort);
        }
        for (int i = 0; i < list.length; i++) {
            ArrayList<Integer> arr = new ArrayList<>();
            //此时arr里面装入的是优先数由高到低排序好的数组进入内存的下标
            arr = return_list(list);
            //根据输入顺序进行排序，否则输出将会是根据优先级由高到低进行排序列的数组
            //将影响后续的操作
            for (int j = 0; j < list.length; j++) {
                for (int k = 0; k < list.length - j - 1; k++) {
                    if (list[k].sort > list[k + 1].sort) {
                        List t = list[k];
                        list[k] = list[k + 1];
                        list[k + 1] = t;
                    }
                }
            }
            int pos = return_pos(arr, list);
            sum_memory += list[pos].return_space;
            arrayList.remove(new Integer(pos));
            System.out.print(list[pos].name + " ");
        }
    }

    public static ArrayList<Integer> return_list(List[] list) {
        //将优先级进行降序排序
        Arrays.sort(list, new Comparator<List>() {
            @Override
            public int compare(List o1, List o2) {
                return o2.priority - o1.priority;
            }
        });
        for (int i = 0; i < list.length; i++) {
            //
            if (list[i].space > 0 && list[i].space <= sum_memory) {
                arrayList.add(list[i].sort);
                sum_memory -= list[i].space;
                list[i].return_space = list[i].space;
                list[i].space = 0;
            }
        }
        //按升序排序
        Collections.sort(arrayList);
        return arrayList;
    }

    public static int return_pos(ArrayList<Integer> arrayList, List[] list) {
        int min_time = Integer.MAX_VALUE;
        int pos = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            if (min_time > list[arrayList.get(i)].time) {
                min_time = list[arrayList.get(i)].time;
                pos = arrayList.get(i);
            }
        }
        return pos;
    }
}
